<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page language="java" import="java.util.*,data.UserInfo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Update user information</title>
    <style type ="text/css">
        .style{
            background-color:#EDF6EA;
            border:1px solid #000000;
        }

        .widthed{ 
            width:100%; 
        }
        .tbwidth1{
            width:160px;
        }
        .tbwidth2{
            width:200px;
        }
        .font{
            font-weight:bold;
        }
        .red {color: #FF0000}
        .none{display: none;}
        .inline{display: inline;}
    </style>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
    <script>
        $(document).ready(function(){
            $("#quo").focusout(function(){
                   var qcheckbox = document.getElementById("qcheck");
                   if(qcheckbox.checked == false){
                       var Quota = document.userform.quota.value;
                       var quota = parseInt(Quota);
                       //alert(Quota + " " + quota);
                       
                       if(Quota != "UNLIMITED" && isNaN(quota)){
                           //alert(quota);
                           $("#errQuota").css('display','block');
                       }else if(Quota < 0){
                           $("#errQuota").text("Quota cannot be negative!!")
                           $("#errQuota").css('display','block');
                       }else{
                           $("#errQuota").fadeOut(1000);
                       }
                   }
                });
                $("#qcheck").change(
                function () 
                {   

                    var qcheckbox = document.getElementById("qcheck");
                    var quota = document.getElementById("quo");
                    if(qcheckbox.checked == true){
                        quota.value = "UNLIMITED";
                        $('#quo').attr("readonly", true); 
                        $('#errQuota').fadeOut(1000);
                    }else{
                        $('#quo').removeAttr("readonly");
                    }

                });
                $('input[name="Submit"]').click(function(event){
                    //event.preventDefault();
                    var isChecked = document.userform.checkbox.checked;
                    var field = document.getElementById("hide");
                    if(isChecked == true){
                        field.value = "LOCK";
                    }else{
                        field.value = "UNLOCK";
                    }
                    //$post("Add")
                });
        });
    </script>
<jsp:useBean id="thisuser" class="data.UserInfo" scope="session"/>
    <%
    System.out.println("updateUser.jsp");
    %>
<form name="userform" method="post" action="Update">
<br><br><br>

<table align="center" width="360px" class ="style">

<tr><td colspan=2 style="font-weight:bold;" align="center">Edit User</td></tr>
<tr><td colspan=2 align="center" height="10px"></td></tr>
	<tr>
            <td class ="tbwidth1 font">ID</td>
		<td class ="tbwidth2">
                    <input type="text" name="ID" class ="widthed"
                           value="<jsp:getProperty name="thisuser" property="ID"/>" readonly>
                    
                </td>
	</tr>
	
	<tr>
            <td class ="tbwidth1 font">UserName</td>
		<td class ="tbwidth2">
                    <input type="text" name="username" class ="widthed"
                           value="<jsp:getProperty name="thisuser" property="username"/>" readonly>
                </td>
	</tr>
	
	
	<tr>
		<td class ="font tbwidth1">Password</td>
		<td class ="tbwidth2"><input type="password" name="password" class ="widthed"></td>
	</tr>
        
	
        <tr>
                <td class ="font tbwidth1">Default tablespace</td>
                <td class ="tbwidth2">
                    
                    <select id ="def" class="widthed" name ="defTbls">
                        <%
                        ArrayList<String> defLst = (ArrayList<String>) session.getAttribute("defTbsList");
                        UserInfo inf = (UserInfo)session.getAttribute("thisuser");
                        String usrname = inf.getDefTbspace();
                        //System.out.println("usrname = " + usrname);
                        for(int i = 0; i < defLst.size(); i++){
                            String name = defLst.get(i);
                            if(name.equals(usrname)){
                                //System.out.println("name = " + name);
                                
                        %>
                        
                        <option value="<%=name%>" selected="selected"><%=name%></option>
                        <%
                            }else{
                        %>
                        <option value="<%=name%>"><%=name%></option>
                        <%
                            }
                        }
                        %>
                    </select>
                    
                </td>
        </tr>
        <tr>
                <td class ="font tbwidth1">Temporary tablespace</td>
                <td class =" tbwidth2">
                    <select id ="tmp" class="widthed" name ="tmpTbls">
                        <%
                        
                        ArrayList<String> tmpLst = (ArrayList<String>) session.getAttribute("tmpTbsList");
                        for(int i = 0; i < tmpLst.size(); i++){
                            String tmpName = tmpLst.get(i);
                            if(tmpName.equals(inf.getTmpTbspace())){
                                //System.out.println(tmpName);
                        %>
                        <option value="<%=tmpName%>" selected="selected"><%=tmpName%></option>
                        <%
                            }else{
                        %>
                        <option value="<%=tmpName%>"><%=tmpName%></option>
                        <%
                            }
                        }
                        %>
                    </select>
                </td>
        </tr>
        <tr>
		<td class ="font tbwidth1">Quota</td>
		<td class ="tbwidth2">
                    <%
                        String quota = inf.getQuota();
                        System.out.println("QUOTA = " + quota);
                    %>
                    <input type="text" id="quo" name="quota" class ="widthed" value ="<%=quota%>">
                     
                    <div id ="errQuota" class="none red"> Wrong quota type (number only)</div>
                </td>
                
	</tr>
        <tr>
            <td></td>
            <td>
                <%
                    
                    
                    if(quota.equals("UNLIMITED")){
                %>
                <input id ="qcheck" type="checkbox" name="qCheckbox" checked ="checked">
                <%
                    }else{
                %>
                <input id ="qcheck" type="checkbox" name="qCheckbox">
                <%    
                    }
                %>
                <div id ="unlimit" class ="inline" >Unlimited</div>
            </td>
        </tr>
	<tr>
		<td class ="font tbwidth1">Account status</td>
                <td class =" tbwidth2">
                    <input  id ="hide" type="hidden" name ="status" class ="widthed">
                    <%
                        String accstt = inf.getAccStatus();
                        if(accstt.equals("LOCK")){
                    %>
                        <input type="checkbox" name="checkbox" checked ="checked">
                    <%
                        }else{
                    %>
                        <input type="checkbox" name="checkbox">
                    <%
                        }
                    %>
                </td>
                
	</tr>
        <tr>
                <td class ="font tbwidth1">Profile</td>
                <td class =" tbwidth2">
                    <select id ="pro" class="widthed" name ="profileList">
                        <%
                        
                        ArrayList<String> profileList = (ArrayList<String>) session.getAttribute("profileList");
                        System.out.println("Profile = " + inf.getProfile());
                        UserInfo u = (UserInfo)session.getAttribute("user");
                        System.out.println(u.getUsername());
                        for(int i = 0; i < profileList.size(); i++){
                            String name = profileList.get(i);
                            
                            if(name.equals(inf.getProfile())){
                                System.out.println(name);
                        %>
                        <option value="<%=name%>" selected ="selected"><%=name%></option>
                        <%
                            }else{
                        %>
                        <option value="<%=name%>"><%=name%></option>
                        <%
                            }
                        }
                        %>
                    </select>
                </td>
        </tr>
        
	<tr>
		
		<th colspan="2"><input type="submit" name="Submit" value="Update" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></th>
	</tr>
	<tr><td colspan=2 align="center" height="10px"></td></tr>
</table>
</form>
    <%System.out.println("------------------------");%>
</body>
</html>